package com.qst.dms.ui;

import java.awt.*;
import java.awt.EventQueue;
import java.awt.event.*;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.border.EmptyBorder;

import com.qst.dms.entity.User;
import com.qst.dms.net.SendEmail;
import com.qst.dms.service.UserService;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.JButton;

public class LoginFrame extends JFrame {
	
	private JPanel contentPane;
	private JTextField userName;			//用户名
	private JPasswordField passWord;		//密码
	private JButton okBt,resBt,regBt;
	// 邮件服务
	private SendEmail mailService;
	private JButton findBt;
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginFrame frame = new LoginFrame();
					frame.setTitle("用户登录界面");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public LoginFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));// ?
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);
		
		JLabel label = new JLabel("用户名：");
		label.setBounds(52, 51, 60, 18);
		label.setForeground(new Color(0, 0, 0));//?
		panel.add(label);
		
		userName = new JTextField();
		userName.setBounds(126, 48, 216, 24);
		panel.add(userName);
		userName.setColumns(10);//?
		
		JLabel label_1 = new JLabel("密码：");
		label_1.setBounds(68, 105, 72, 18);
		panel.add(label_1);
		
		passWord = new JPasswordField();
		passWord.setBounds(126, 102, 216, 24);
		panel.add(passWord);
		passWord.setColumns(10);
		
		okBt = new JButton("登录");
		okBt.addActionListener(new LoginListener());
		okBt.setBounds(90, 172, 72, 27);
		panel.add(okBt);
		
		resBt = new JButton("重置");
		resBt.addActionListener(new ResetListener());
		resBt.setBounds(176, 172, 72, 27);
		panel.add(resBt);
		
		regBt = new JButton("注册");
		regBt.addActionListener(new RegistListener());
		regBt.setBounds(262, 172, 72, 27);
		panel.add(regBt);
		
		findBt = new JButton("找回密码");
		findBt.addActionListener(new FindPasswordListener());
		findBt.setBounds(348, 172, 72, 24);
		panel.add(findBt);
	}
	
	// 监听类，负责处理确认按钮的业务逻辑
	private class LoginListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String user = userName.getText().trim();
			String pwd = new String(passWord.getPassword());
			//String password = new String(txtPwd.getPassword());
			UserService service = new UserService();
			try{
				User temp = service.findUserByName(user);
				if(temp == null){
					JOptionPane.showMessageDialog(null,"账号错误！","错误提示",JOptionPane.ERROR_MESSAGE);
				}
				else if(temp.getPassword().equals(pwd)){
					JOptionPane.showMessageDialog(null,"登录成功！","成功提示",JOptionPane.PLAIN_MESSAGE);
					LoginFrame.this.setVisible(false);
					new MainFrame();
				}
				else{
					JOptionPane.showMessageDialog(null,"密码错误！","错误提示",JOptionPane.ERROR_MESSAGE);
				}
			}catch(Exception ee){
				ee.printStackTrace();
			}			
		}
	}
	
	// 监听类，负责处理重置按钮
	private class ResetListener implements ActionListener {
		// 重写actionPerFormed()方法，重置组件内容事件处理方法
		public void actionPerformed(ActionEvent e) {
			// 清空姓名、密码、确认密码内容
			userName.setText("");
			passWord.setText("");
		}
	}
	
	// 监听类，负责处理注册按钮
	private class RegistListener implements ActionListener{
		//重写actionPerFormed()方法，调用注册界面事件处理方法
		public void actionPerformed(ActionEvent e){
			new RegistFrame();
			
		}
	}

	// 监听器，负责处理找回密码按钮
	public class FindPasswordListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			SendEmail mailService = new SendEmail();
			UserService userService = new UserService();
			String username = userName.getText().toString();
			
			User user = userService.findUserByName(username);
			if(user != null) {
				if(true == mailService.sendMail(username, user.getPassword())) {
					JOptionPane.showMessageDialog(null,"找回密码成功，您的密码已发送到你的邮箱，请及时查看。","成功提示",JOptionPane.PLAIN_MESSAGE);					
				}
			}else {
				JOptionPane.showMessageDialog(null,"找回密码失败！","失败提示",JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
}
